﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Page_Good_Edit : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadGoodType();             //商品类别
            LoadGoodBrand();            //商品品牌

            ReadAction();               //保存传过来的参数 
            IsCheck();                  //判断用户状态：新增|修改
            
            btnSave.Attributes.Add("onclick", "return Regular()"); //添加校验代码   
        }
    }

    #region Web 窗体设计器生成的代码

    protected override void OnLoad(EventArgs e)
    {
        isRequestLogin = true;                                  //是否需要登录
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url
        isAtFramePageLogin = true;                              //是框架页

        //RequestCompetences = Competences.BuildCompetencesList(Competences.DAGL_71);//页面权限

        base.OnLoad(e);
    }

    #endregion

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Good_Manager.aspx");
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        long GoodID = Shove._Convert.StrToLong(hID.Value,0);
        DAL.Tables.tbl_Shop_Good goods = new DAL.Tables.tbl_Shop_Good();

        string fileName = "";
        string url = "~/UpFile/Goods/";

        if (this.FilePic_Small.Value != "")
        {
            if (Shove._IO.File.UploadFile(this.Page, this.FilePic_Small, url, ref fileName, "") < 0)
            {
                Shove._Web.JavaScript.Alert(this.Page, "文件上传失败");
            }

            goods.GoodPic_Small.Value = fileName;
            goods.GoodPic_Big.Value = fileName;
        }

        goods.GoodNo.Value = tbGoodNO.Text;
        goods.GoodName_CN.Value = tbGoodName_CN.Text;
        goods.GoodName_EN.Value = tbGoodName_EN.Text;
        goods.BrandID.Value = Shove._Convert.StrToInt((ddlGoodBrand.SelectedValue), 0);
        goods.GoodTypeID.Value = ddlGoodType.SelectedValue;
        goods.Price_Orig.Value = Shove._Convert.StrToDouble(tbPrice_Orig.Value.ToString(),0);
        goods.Price_Vip.Value = Shove._Convert.StrToDouble(tbPrice_Vip.Value.ToString(), 0);
        goods.Price_BV.Value = Shove._Convert.StrToDouble(tbPrice_BV.Value.ToString(), 0);
        goods.Weight.Value = Shove._Convert.StrToDouble(tbWeight.Value.ToString(), 0);
        goods.GoodUnit.Value = tbGood_Unit.Text;
        goods.Price_Lastin.Value = Shove._Convert.StrToDouble(tbPrice_Lastin.Value.ToString(), 0);
        goods.ShopID.Value = _Site.ID;
        ////是否自销
        //if (cbIsSelf.Visible == true && cbIsSelf.Checked == false)
        //{
        //    goods.ShopID.Value = -1;
        //}
        //else
        //{
            
        //}

        try
        {
            goods.GoodNorm.Value = tbGoodNorm.Text;
        }
        catch { }
        try
        {
            goods.GoodColor.Value = tbGoodColor.Text;
        }
        catch { }

        goods.GoodIntroduce.Value = this.fckContent.Value;
        goods.GoodIntroduce_Detail.Value = tbContent.Value;   

        if (this.hAction.Value == "Edit")           //修改
        {
            goods.Update("ID=" + GoodID);
            Shove._Web.JavaScript.Alert(this.Page, "修改成功！", "Good_Manager.aspx");
        }
        if (this.hAction.Value == "Add")            //添加
        {
            goods.Insert();
            Shove._Web.JavaScript.Alert(this.Page, "添加成功！", "Good_Manager.aspx");
        }
    }

    #region 自定义方法

    /// <summary>
    /// 保存传过来的参数
    /// </summary>
    private void ReadAction()
    {
        hAction.Value  = Shove._Web.Utility.GetRequest("Action");
        hID.Value = Shove._Web.Utility.GetRequest("GoodID");

        //if (_Site.ID != 1)
        //{
        //    cbIsSelf.Checked = true;
        //    cbIsSelf.Enabled = false;
        //}
    }

    /// <summary>
    /// 判断用户状态：新增|修改
    /// </summary>
    private void IsCheck()
    {
        if (this.hAction.Value == "Edit")
        {
            DispLayOut();
        }
    }

    /// <summary>
    /// 梆定商品类别信息
    /// </summary>
    private void LoadGoodType()
    {
        ddlGoodType.DataSource = new DAL.Tables.tbl_Shop_GoodType().Open("", "[TypeStatus]=1 ", "");    //and (ShopID=-1 or ShopID=" + _Site.ID + ") 
        ddlGoodType.DataTextField = "TypeName";
        ddlGoodType.DataValueField = "ID";

        ddlGoodType.DataBind();
        ddlGoodType.Items.Insert(0, "--商品类别--");
    }

    /// <summary>
    /// 梆定商品品牌信息
    /// </summary>
    private void LoadGoodBrand()
    {
        ddlGoodBrand.DataSource = new DAL.Tables.tbl_Shop_Brand().Open("", "Isuse = 1 ", "");   //and (ShopID=-1 or ShopID=" + _Site.ID + ")
        ddlGoodBrand.DataTextField = "BrandName_CN";
        ddlGoodBrand.DataValueField = "ID";

        ddlGoodBrand.DataBind();
        ddlGoodBrand.Items.Insert(0, "--商品品牌--");
    }

    /// <summary>
    /// 编辑状态时，初始化
    /// </summary>
    private void DispLayOut()
    {
        DataTable dt = new DataTable();
        dt = new DAL.Tables.tbl_Shop_Good().Open("", "[ID]=" + hID.Value, "");
        if (dt != null)
        {
            tbGoodNO.Text = dt.Rows[0]["GoodNo"].ToString();
            tbGoodName_CN.Text = dt.Rows[0]["GoodName_CN"].ToString();
            tbGoodName_EN.Text = dt.Rows[0]["GoodName_EN"].ToString();
            ddlGoodBrand.SelectedValue = dt.Rows[0]["BrandID"].ToString();
            ddlGoodType.SelectedValue = dt.Rows[0]["GoodTypeID"].ToString();

            tbPrice_Orig.Value = dt.Rows[0]["Price_Orig"].ToString();
            tbPrice_Vip.Value = dt.Rows[0]["Price_Vip"].ToString();
            tbPrice_Lastin.Value = dt.Rows[0]["Price_Lastin"].ToString();
            tbPrice_BV.Value = dt.Rows[0]["Price_BV"].ToString();
            tbWeight.Value = dt.Rows[0]["Weight"].ToString();
            tbGood_Unit.Text = dt.Rows[0]["GoodUnit"].ToString();

            string fileName = dt.Rows[0]["GoodPic_Small"].ToString();
            string url = "~/UpFile/Goods/"+fileName;
            ImageBig.ImageUrl = url;

            try
            {
                tbGoodNorm.Text = dt.Rows[0]["GoodNorm"].ToString();
            }
            catch { }
            try
            {
                tbGoodColor.Text = dt.Rows[0]["GoodColor"].ToString();
            }
            catch { }
           
            tbContent.Value = dt.Rows[0]["GoodIntroduce_Detail"].ToString();
            fckContent.Value = dt.Rows[0]["GoodIntroduce"].ToString();
        }
    }

    #endregion
}